package dongze.com.ckdj.ui;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.support.annotation.StyleRes;
import android.view.View;

/**
 * Created by fanqwz on 2017/8/3.
 */

public class StateDialog extends AlertDialog {
    String message;
    boolean dismiss = false;
    boolean exit = false;
    Activity activity;

    public StateDialog(Context context) {
        super(context);
        activity = (Activity) context;
        setButton(BUTTON_POSITIVE, "OK", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
    }

    protected StateDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected StateDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    public void show() {
        super.show();
        getButton(BUTTON_POSITIVE)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dismiss) {
                            if (exit)
                                activity.finish();
                            dismiss();
                        } else {
                            show();
                        }
                    }
                });
    }

    public void addStateMessage(String str) {
        if (message == null)
            message = str;
        else
            message += "\n" + str;

        setMessage(message);
    }

    public void critical(String str) {
        addStateMessage(str);
        dismiss = true;
        exit = false;
    }

    public void finish() {
        dismiss = true;
        exit = true;
    }


}
